/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Converters;

import ComponenteEntidad.Egresos;
import ManagedBeans.ManagedBeanEgreso;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
/**
 *
 * @author Noemi
 */
@FacesConverter(value = "EgresoConverter")
public class EgresoConverter implements Converter{
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (context == null) {
            throw new NullPointerException("context");
        }
        if (component == null) {
            throw new NullPointerException("component");
        }
        FacesContext ctx = FacesContext.getCurrentInstance();
        ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(), "#{managedBeanEgreso}", ManagedBeanEgreso.class);
        ManagedBeanEgreso objetomanagebeans = (ManagedBeanEgreso) vex.getValue(ctx.getELContext());
        Egresos objeto;
        try {
            objeto = objetomanagebeans.getObjtetoEgresos(new Integer(value));
        } catch (NumberFormatException e) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Valor Desconocido", "Codigo Incorrecto");
            throw new ConverterException(message);
        }
        if (objeto == null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Valor Desconocido", "El Objeto es Desconocido");
            throw new ConverterException(message);
        }
        return objeto;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (context == null) {
            throw new NullPointerException("context");
        }
        if (component == null) {
            throw new NullPointerException("component");
        }
        if (((Egresos) value).getIdEgresos()!= null) {
            return ((Egresos) value).getIdEgresos().toString();
        } else {
            return null;
        }
    }
    
}
